package com.aaa.controller.user;

import com.aaa.entity.Card;
import com.aaa.entity.ResponseDto;
import com.aaa.entity.User;
import com.aaa.service.UserService;
import com.aaa.service.impl.UserServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 会员登记
@WebServlet("/AddUserServlet")
public class AddUserServlet extends HttpServlet {
     private UserService userService;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String userName = req.getParameter("userName");
        String phone = req.getParameter("phone");
        String level = req.getParameter("levelId");
        String status = req.getParameter("status");
        String staffId = req.getParameter("staffId");
        String credit = req.getParameter("credit");
        String idCard = req.getParameter("idCard");
        String address = req.getParameter("address");
        String sex = req.getParameter("sex");
        String amount = req.getParameter("amount");
        String momo = req.getParameter("momo");
        String area = req.getParameter("area");

        User user = new User();
        Card card = new Card();
        user.setAddress(address);
        user.setUserName(userName);
        user.setIdCard(idCard);
        user.setPhone(phone);
        user.setStaffId(Integer.parseInt(staffId));
        user.setSex(Integer.parseInt(sex));
        user.setStatus(Integer.parseInt(status));
        user.setMomo(momo);
        user.setArea(area);
        card.setAmount(Double.parseDouble(amount));
        card.setCredit(Integer.parseInt(credit));
        card.setLevelId(Integer.parseInt(level));
        card.setStatus(Integer.parseInt(status));
        card.setStaffId(Integer.parseInt(staffId));
        int num = userService.addUser(user,card);

        ResponseDto responseDto = new ResponseDto();
        if (num > 0){
            responseDto.setStatus(ResponseDto.SUCCESS_CODE);
            responseDto.setMessage("添加成功");
        }else {
            responseDto.setStatus(ResponseDto.FAILURE_CODE);
            responseDto.setMessage("添加失败");
        }
        resp.getWriter().write(new Gson().toJson(responseDto));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    public void init() throws ServletException {
        userService = new UserServiceImpl();
    }

}
